/*********************************************************
PROGRAM: 00_MW_GetACS.sas
PURPOSE: Pulls non-elderly adults in households with low income
AUTHOR:	 Sarah Miller
DATE: 	 June 25, 2018
*********************************************************/
libname outdata "/projects/mortality/data";
libname innum "/data/adrec/census_cnum/2018/";


data outdata.numident;
set innum.cnum_2018q2;
if dodcc="19" then delete;
run;

proc sort data=outdata.numident;
by pik;
quit;


%macro readit (year=);
libname indata "/data/demographic/acs/&year./";
libname inxw "/data/adrec/census_crosswalk_acs/&year./";

%if &year LT 2016 %then %do;

proc contents data=indata.vpers&year._1yr;
run;

data outdata.pers&year;
set indata.vpers&year._1yr;
        age2014=(2014-&year)+age;
        run;
        
%end;
%else %do;
proc contents data=indata.acs&year._vpers_1yr;
run;

data outdata.pers&year;
	set indata.acs&year._vpers_1yr;
        age2014=(2014-&year)+age;
        run;
        
%end;

proc sort data=outdata.pers&year;
by cmid pnum;
quit;

data crosswalk&year;
set inxw.crosswalk_acs&year;
run;

proc sort data=crosswalk&year;
by cmid pnum;
run;

data outdata.pers&year;
merge outdata.pers&year (in=a) crosswalk&year;
by cmid pnum;
if a;
run;

proc sort data=outdata.pers&year;
by pik;
quit;

data outdata.pers&year;
merge outdata.pers&year (in=a)  outdata.numident;
by pik;
if a;
if pik=" " then delete;
if dodyy=" " then died=0;
else died=1;
dy=dodyy*1;
if died=0 then deathyear=9999;
else deathyear=2000+dy;
run;

proc export data=outdata.pers&year outfile="/projects/mortality/data/pers&year..dta" replace;
run;

%mend readit;

%readit (year=2008);
%readit (year=2009);
%readit (year=2010);
%readit (year=2011);
%readit (year=2012);
%readit (year=2013);

